package com.azunitech.jms.ejb.unit;

import static org.junit.Assert.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ejb.embeddable.EJBContainer;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.NamingException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.azunitech.jms.ejb.HelloService;

public class Unit_HelloService_Greeting_General {
    private static EJBContainer ejbContainer;

    private static Context ctx;
    
    @Resource
    private ConnectionFactory connectionFactory;
    
    @Resource(name = "Q-001")
    private Queue questionQueue;
    
    @Before
    public void setUpClass() throws Exception {
        // Instantiate an embeddable EJB container and search the
        // JVM class path for eligible EJB modules or directories
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(EJBContainer.MODULES, 
        		new File[]{
        			new File("target/classes"), 
        			new File("target/test-classes"), 
        			new File("target/lib/jms-ejb-common-1.0-SNAPSHOT.jar"),
        			new File("target/lib/jms-ejb-business-1.0-SNAPSHOT.jar"),
        			new File("target/lib/jms-ejb-repository-1.0-SNAPSHOT.jar"),
        			new File("target/lib/jms-ejb-util-1.0-SNAPSHOT.jar")
        		}
        );
 
        ejbContainer = EJBContainer.createEJBContainer();

        // Get a naming context for session bean lookups
        ctx = ejbContainer.getContext();
        ctx.bind("inject", this);
    }

    @After
    public void tearDownClass() throws Exception {
        // Shutdown the embeddable container
        ejbContainer.close();
    }

    //@Test
    public void hello() throws NamingException {
        // Retrieve a reference to the session bean using a portable
        // global JNDI name
    	HelloService helloWorld = (HelloService) ctx.lookup("java:global/classes/HelloServiceImpl");

        // Do your tests
        assertNotNull(helloWorld);
        String expected = "World";
        String hello = helloWorld.greet(expected);
        assertNotNull(hello);
        assertTrue(hello.endsWith(expected));
    }
}


